package hot100.logos51To60;

import java.util.ArrayList;
import java.util.List;

/**
 * @author logos
 * date 2025/7/15 12:52
 * @version v1.0
 * @Package hot100.logos51To60
 */
public class logos55 {

    /**
     * 55. 全排列
     * https://leetcode.cn/problems/permutations/description/?envType=study-plan-v2&envId=top-100-liked
     */

    class Solution {
        public List<List<Integer>> permute(int[] nums) {
            List<List<Integer>> ans = new ArrayList<>();
            solve(nums, 0, ans);
            return ans;
        }

        private void solve(int[] nums, int i, List<List<Integer>> ans) {
            if (i == nums.length) {
                List<Integer> tmp = new ArrayList<>();
                for (int x : nums) {
                    tmp.add(x);
                }
                ans.add(tmp);
            } else {
                for (int j = i; j < nums.length; j++) {
                    swap(nums, i, j);
                    solve(nums, i + 1, ans);
                    swap(nums, i, j);
                }
            }
        }

        private void swap(int[] nums, int i, int j) {
            int tmp = nums[i];
            nums[i] = nums[j];
            nums[j] = tmp;
        }
    }
}
